/* $Id: ContestLanguagesModel.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.models;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;

import alt.djudge.frontend.server.datatypes.ContestEntry;
import alt.djudge.frontend.server.datatypes.ContestLanguageEntry;
import alt.djudge.frontend.server.datatypes.LanguageEntry;


public class ContestLanguagesModel
{
	private static final ModelSettings<ContestLanguageEntry> settings = new ModelSettings<ContestLanguageEntry>()
	{
		@Override
		public Order getOrdering()
		{
			return Order.asc("id");
		}
		
		@Override
		public Class<ContestLanguageEntry> getEntryClass()
		{
			return ContestLanguageEntry.class;
		}
	}; 
	
	private static final CoreInterface<ContestLanguageEntry> core = new DBAbstractor<ContestLanguageEntry>(settings).getModel();
	
	public static List<ContestLanguageEntry> getAllEntries()
	{
		return (List<ContestLanguageEntry>) core.getAllEntries();
	}

	public static List<ContestLanguageEntry> getEntries(Map<String, Object> map)
	{
		return (List<ContestLanguageEntry>) core.getEntries(map);
	}

	public static List<LanguageEntry> getContestEntries(Long contestId)
	{
		List<LanguageEntry> list = null;
		ContestEntry contest = ContestsModel.getEntry(contestId);
		if (contest.getAllowAllLanguages())
		{
			list = LanguagesModel.getAllEntries();
		}
		else
		{
			HashMap<Long, LanguageEntry> languages = LanguagesModel.getEntriesMap();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("contestId", contestId);
			List<ContestLanguageEntry> list2 = (List<ContestLanguageEntry>) core.getEntries(map);
			list = new ArrayList<LanguageEntry>();
			for (ContestLanguageEntry contestLanguageEntry : list2)
				list.add(languages.get(contestLanguageEntry.getLanguageId()));
		}
		return list;
	}

	public static ContestLanguageEntry getEntry(Long contestLanguageId)
	{
		return (ContestLanguageEntry) core.getEntry(contestLanguageId);
	}
	
	public static boolean deleteEntry(Long contestProblemId)
	{
		return core.deleteEntry(contestProblemId);
	}
	
	public static CoreInterface<ContestLanguageEntry> getCore()
	{
		return core.getAdapter();
	}
}
